package zakladki.AS3.menu {
import mx.controls.Alert;
import mx.core.FlexGlobals;
import mx.utils.LinkedList;
import mx.utils.LinkedListNode;

import zakladki.AS3.menu.MenuButtonRequest;
import zakladki.AS3.menu.MenuButtonRequest;

public class MenuActionListener {

    private var rootApp:Fossilminator = FlexGlobals.topLevelApplication as Fossilminator;

    private var houseButtons:MenuButtonRequest;
    private var warmWaterButtons:MenuButtonRequest;
    private var publicTransportButtons:MenuButtonRequest;
    private var privateTransportButtons:MenuButtonRequest;
    private var foodButtons:MenuButtonRequest;
    private var consumptionButtons:MenuButtonRequest;
    private var emissionButtons:MenuButtonRequest;

    private var menuList:LinkedList;

    public function MenuActionListener(houseButtons:MenuButtonRequest, warmWaterButtons:MenuButtonRequest, publicTransportButtons:MenuButtonRequest, privateTransportButtons:MenuButtonRequest, foodButtons:MenuButtonRequest, consumptionButtons:MenuButtonRequest, emissionButtons:MenuButtonRequest) {
        this.houseButtons = houseButtons;
        this.warmWaterButtons = warmWaterButtons;
        this.publicTransportButtons = publicTransportButtons;
        this.privateTransportButtons = privateTransportButtons;
        this.foodButtons = foodButtons;
        this.consumptionButtons = consumptionButtons;
        this.emissionButtons = emissionButtons;

        this.buildList();
    }

    private function buildList():void {
        menuList = new LinkedList();
        menuList.push(houseButtons);
        menuList.push(warmWaterButtons);
        menuList.push(privateTransportButtons);
        menuList.push(publicTransportButtons);
        menuList.push(foodButtons);
        menuList.push(consumptionButtons);
        menuList.push(emissionButtons);
    }

    private function changeNodeView(request:MenuButtonRequest):void {
        var currentNode:LinkedListNode = menuList.find(request);
        if(currentNode != null){
            var reqButton:MenuButtonRequest = currentNode.value;

            rootApp.menuButtonClicked(reqButton.bookmarkNumber, reqButton.appViewNumber);

            var prevNode:LinkedListNode = currentNode.prev;
            var nextNode:LinkedListNode = currentNode.next;

            if(prevNode.prev != null){
                setPreviousButtonVisibility(prevNode.value);
            }

            if(nextNode.next != null){
                setCurrentButtonVisibility(reqButton);

                var nextBtnReq:MenuButtonRequest = nextNode.value;
                if(rootApp.isBookmarkInitalized(nextBtnReq.bookmarkNumber)){
                    reqButton.arrowButton2.visible = true;
                }
                else {
                    reqButton.arrowButton1.visible = true;
                }
            }
            else { //Emission button clicked
                reqButton.labelButton1.visible = false;
                reqButton.labelButton2.visible = false;
                reqButton.labelButton3.visible = true;
            }
        }
    }

    private function traverseButtonList():void {
        var currentNode:LinkedListNode = menuList.head;
        while(currentNode.next != null){
            var menuButtonReq:MenuButtonRequest = currentNode.value;
            if(currentNode != menuList.tail){
                setButtonsVisibility(menuButtonReq, false);
                changeTraversalRegularButton(currentNode);
            }
            else {
                changeTraversalEmissionButton(menuButtonReq);
            }
            currentNode = currentNode.next;
        }
    }

    private function changeTraversalRegularButton(buttonNode:LinkedListNode):void {
        var buttonRequest:MenuButtonRequest = buttonNode.value;

        if(rootApp.isBookmarkInitalized(buttonRequest.bookmarkNumber)){
            buttonRequest.labelButton1.visible = true;
            var nextButtonNode:LinkedListNode = buttonNode.next;
            if(nextButtonNode != null){
                var nextButtonReq:MenuButtonRequest = nextButtonNode.value;
                if(rootApp.isBookmarkInitalized(nextButtonReq.bookmarkNumber)){
                    buttonRequest.arrowButton4.visible = true;
                }
                else {
                    buttonRequest.arrowButton3.visible = true;
                }
            }
        }
        else{
            buttonRequest.labelButton2.visible = true;
            buttonRequest.arrowButton8.visible = true;
        }
    }

    private function changeTraversalEmissionButton(emissionButton:MenuButtonRequest):void {
        emissionButton.labelButton1.visible = false;
        emissionButton.labelButton2.visible = false;
        emissionButton.labelButton3.visible = false;

        if(rootApp.isBookmarkInitalized(emissionButton.bookmarkNumber)){
            emissionButton.labelButton1.visible = true;
        }
        else {
            emissionButton.labelButton2.visible = true;
        }
    }

    private function setCurrentButtonVisibility(buttons:MenuButtonRequest):void{
        buttons.labelButton1.visible = false;
        buttons.labelButton2.visible = false;
        buttons.labelButton3.visible = true;

        buttons.arrowButton1.visible = false;
        buttons.arrowButton2.visible = false;
        buttons.arrowButton3.visible = false;
        buttons.arrowButton4.visible = false;
        buttons.arrowButton5.visible = false;
        buttons.arrowButton6.visible = false;
        buttons.arrowButton7.visible = false;
        buttons.arrowButton8.visible = false;
    }

    private function setPreviousButtonVisibility(buttons:MenuButtonRequest):void{
        buttons.arrowButton1.visible = false;
        buttons.arrowButton2.visible = false;
        buttons.arrowButton3.visible = false;
        buttons.arrowButton4.visible = false;
        buttons.arrowButton5.visible = true;
        buttons.arrowButton6.visible = false;
        buttons.arrowButton7.visible = false;
        buttons.arrowButton8.visible = false;
    }

    private function setButtonsVisibility(buttons:MenuButtonRequest,visible:Boolean):void{

        buttons.arrowButton1.visible = visible;
        buttons.arrowButton2.visible = visible;
        buttons.arrowButton3.visible = visible;
        buttons.arrowButton4.visible = visible;
        buttons.arrowButton5.visible = visible;
        buttons.arrowButton6.visible = visible;
        buttons.arrowButton7.visible = visible;
        buttons.arrowButton8.visible = visible;

        buttons.labelButton1.visible = visible;
        buttons.labelButton2.visible = visible;
        buttons.labelButton3.visible = visible;
    }

    public function houseSelected():void {
        traverseButtonList();
        changeNodeView(houseButtons);
    }

    public function warmWaterSelected():void {
        traverseButtonList();
        changeNodeView(warmWaterButtons);
    }

    public function privateTransportSelected():void {
        traverseButtonList();
        changeNodeView(privateTransportButtons);
    }

    public function publicTransportSelected():void {
        traverseButtonList();
        changeNodeView(publicTransportButtons);
    }

    public function foodSelected():void {
        traverseButtonList();
        changeNodeView(foodButtons);
    }

    public function consumptionSelected():void {
        traverseButtonList();
        changeNodeView(consumptionButtons);
    }

    public function emissionSelected():void {
        traverseButtonList();
        changeNodeView(emissionButtons);
    }


}
}
